package com.renhuikeji.pro.ala.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.view.CommonHeader;
import com.renhuikeji.pro.ala.R;
import com.renhuikeji.pro.ala.adapter.GeneralGoodAdapter;
import com.renhuikeji.pro.ala.bean.HomeRecyBean;
import com.renhuikeji.pro.ala.interf.IBackInterface;
import com.renhuikeji.pro.ala.utils.glide.GlideImageLoaderForBanner;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 淘宝天猫
 */

public class TaoBaoFragment extends Fragment {

    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.recycleView)
    LRecyclerView recyclerView;
    @BindView(R.id.floating_btn)
    FloatingActionButton floatingBtn;
    @BindView(R.id.rl_list)
    RelativeLayout rlList;
    Unbinder unbinder;
    private View tbView;
    private IBackInterface backInterface;
    private GeneralGoodAdapter mAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private int page=0;
    private String session;

    private int TOTAL_COUNTER = 20;     //商品总数
    private static int REQUEST_COUNT = 10;    //每次加载数量
    private static int mCurrentCounter = 0;   //当前商品位置
    private int totalPage = 1;          //总页数

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        tbView = inflater.inflate(R.layout.fragment_taobao, null);
        unbinder = ButterKnife.bind(this, tbView);
        initViews();
        return tbView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }



    @Override
    public void onResume() {
        super.onResume();
    }

    private void initViews() {
        tvMiddleTitle.setText("领券直播");
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
        mAdapter = new GeneralGoodAdapter(getActivity());
        mAdapter.setDataList(new ArrayList<HomeRecyBean>());
        mAdapter.setLayoutType(2);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        //设置lrecyclerview
        GridItemDecoration divider = new GridItemDecoration.Builder(getActivity())
                .setHorizontal(R.dimen.line_height)
                .setVertical(R.dimen.line_height)
                .setColorResource(R.color.line_gray)
                .build();
//        recyclerView.addItemDecoration(divider);
        recyclerView.setLoadMoreEnabled(true);
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                mAdapter.clear();
                page=0;
                lRecyclerViewAdapter.notifyDataSetChanged();
                getDatas();
            }
        });
        initHeader();
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
//                if(count<(page*requestNum)){
//                    ToastUtils.toastForShort(getActivity(),getString(R.string.no_more));
//                    recyclerView.setNoMore(true);
//                }else{
//                    page++;
//                    getDatas();
//
//                }
            }
        });
        recyclerView.refresh();
    }

    private void initHeader() {
        CommonHeader header=new CommonHeader(getActivity(),R.layout.head_living);
        Banner banner= (Banner) header.findViewById(R.id.banner);
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
            }
        });
        banner.setImageLoader(new GlideImageLoaderForBanner());
        List<Integer> pics = new ArrayList<>();
        pics.add(R.mipmap.banner);
        pics.add(R.mipmap.banner);
        pics.add(R.mipmap.banner);
        banner.setImages(pics);
        banner.start();
        TabLayout tabLayout = (TabLayout) header.findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("默认"));
        tabLayout.addTab(tabLayout.newTab().setText("即将抢完"));
        tabLayout.addTab(tabLayout.newTab().setText("销量"));
        tabLayout.addTab(tabLayout.newTab().setText("母婴"));
        tabLayout.addTab(tabLayout.newTab().setText("优惠比例"));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {}
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {}
            @Override
            public void onTabReselected(TabLayout.Tab tab) {}
        });
        lRecyclerViewAdapter.addHeaderView(header);
    }




    private void getDatas() {
        List<HomeRecyBean> lists=new ArrayList<>();
        for(int i=0;i<60;i++){
            HomeRecyBean bean=new HomeRecyBean();
//           bean.setImgUrl("http://image.uczzd.cn/2177696036028180340.jpeg?id=0&from=export");
            bean.setImgUrl("http://img.eeyy.com/uploadfile/2015/0617/20150617093142854.jpg");
            bean.setGoodIntroduce("商品---------"+i);
            bean.setNewprices((i*7)+"");
            lists.add(bean);
            mAdapter.addAll(lists);
            recyclerView.refreshComplete(10);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_middle_title, R.id.floating_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_middle_title:
                break;
            case R.id.floating_btn:
                break;
        }
    }
}
